<%@page import="Entity.Student"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="DAO.DAO_Student"%>
<%
    String sid = request.getParameter("id");
    DAO_Student dao = new DAO_Student();
    Student student = null;
    String name = "";
    String sage = "";
    int sex = 0;
    String hometown = "";

    if (sid != null) {
        int id = Integer.parseInt(sid);
        if (request.getParameter("submit") == null) {
            String sql = "SELECT * FROM Student WHERE id = " + id;
            ResultSet rs = dao.getData(sql);
            while(rs.next()) {
                name = rs.getString(2);
                sage = rs.getString(3);
                sex = rs.getInt(4);
                hometown = rs.getString(5);    
            }
            
        } else {
            boolean ok = true;
            name = request.getParameter("name");
            if (name == null || name.isEmpty()) {
                out.println("<p>Name cannot be empty</p>");
                ok = false;
            } else {
                name = new String(name.getBytes("ISO8859_1"), "UTF-8");
                if (name.length() > 50) {
                    out.println("<p>Name must not be greater than 50 characters</p>");
                    ok = false;
                }
            }

            sage = request.getParameter("age");
            int age = 0;
            if (sage == null || sage.isEmpty()) {
                out.println("<p>Age cannot be empty</p>");
                ok = false;
            } else {
                try {
                    age = Integer.parseInt(sage);
                    if (age <= 0 || age >= 200) {
                        out.println("<p>Age must be a positive number between 1 to 199</p>");
                        ok = false;
                    }
                } catch (Exception ex) {
                    out.println("<p>Age must be a positive number between 1 to 199</p>");
                    ok = false;
                }
            }

            String ssex = request.getParameter("sex");
            sex = 0;
            if (ssex == null || ssex.isEmpty()) {
                out.println("<p>Sex cannot be empty</p>");
                ok = false;
            } else {
                sex = Integer.parseInt(ssex);
            }

            hometown = request.getParameter("hometown");
            if (hometown == null || hometown.isEmpty()) {
                out.println("<p>Hometown cannot be empty</p>");
                ok = false;
            } else {
                hometown = new String(hometown.getBytes("ISO8859_1"), "UTF-8");
                if (hometown.length() > 20) {
                    out.println("<p>Hometown must not be greater than 20 characters</p>");
                    ok = false;
                }
            }

            if (ok) {
                student = new Student(id, name, age, sex, hometown);
                dao.updateStudent(student);
                response.sendRedirect("Student.jsp");
            }
        }
%>
<h2 class="sub-header">Update Student with ID: <%=id%></h2>   
<form role="form" method="post" accept-charset="UTF-8">
    <input type="hidden" name="id" value="<%=id%>">
    <div class="form-group">
        <label>Name:</label>
        <input type="text" name="name" value="<%=name%>" class="form-control">
    </div>

    <div class="form-group">
        <label>Age:</label>
        <input type="text" name="age" value="<%=sage%>" class="form-control" placeholder="Enter age">
    </div>  
    <label>Sex:</label>
    <label class="radio-inline">
        <input type="radio" name="sex" <% if (sex == 0) {
                out.print("checked='true'");
            } %> value="0"> Female
    </label>
    <label class="radio-inline">
        <input type="radio" name="sex" <% if (sex == 1) {
                out.print("checked='true'");
            } %> value="1"> Male
    </label>
    <label class="radio-inline">
        <input type="radio" name="sex" <% if (sex == 2) {
                out.print("checked='true'");
            }%> value="2"> Unknown
    </label>

    <div class="form-group">
        <label>Hometown:</label>
        <input type="text" name="hometown" value="<%=hometown%>" class="form-control" placeholder="Enter hometown">
    </div>      
    <button type="submit" name="submit" class="btn btn-primary btn-lg" style="border-radius: 0"><i class="fa fa-pencil-square-o"></i> Update Student</button>
</form>
<%
    }
%>